<template>
  <div class="card-page">
    <tool-bar nav="" :title="action_name + '自定义Dockerfile'">
      <el-button size="small" @click="$router.push('/dev/user-dockerfile/add')" v-if="$props.action=='edit'">新建
      </el-button>
      <el-button size="small" @click="save_click" type="primary">保存</el-button>
    </tool-bar>
    <el-row :gutter="12" type="flex">
      <el-col style="flex:1">
        <el-card shadow="always">
          <kv label="name">
            <el-input v-model="info.name" chk="*"/>
          </kv>

          <kv label="备注">
            <el-input v-model="info.remark" type="textarea"/>
          </kv>

        </el-card>

        <el-card shadow="always">
          <kv>
            <template #k>
              <span class="k">参考Dockerfile模板</span></template>
              <sect style="display: flex; justify-content: space-between;" ref="lang">
                <selector v-model.trim="info.language" enum="JenkinsLanguageEnum"
                          chk="*"/>
                <selector v-if="info.language" style="margin-left:6px;" :enum-count="1"
                          v-model.trim="info.languageVersion" @change="version_change"
                          :data="versions"
                          chk="*"/>
              </sect>
          </kv>

          <el-input wrap="off" readonly type="textarea" disabled
                    :autosize="{ minRows: 4, maxRows: 16 }"
                    v-model="info.templateContent"
                    :key="$data.version"/>

        </el-card>
      </el-col>
      <el-col style="flex:1">
        <el-card shadow="always" v-if="info.templateContent">
          <el-input v-model="info.dockerfile" type="textarea" :rows="27"></el-input>

        </el-card>
      </el-col>
    </el-row>
    <!--        <txt-editor v-model="$data.test" :content="info.templateContent"></txt-editor>-->
  </div>
</template>
<style scoped lang="scss">
::v-deep(.editor-error) textarea {
  color: red;
  padding: 40px;
  font-size: large;
}
</style>
<script setup>
/**
 * Created by CodeGenerator  at 2022-09-07 13:54:56
 */
import TxtEditor from "@/component/txt-editor.vue"





//定义 vue2的this,当前组件
const $self = getCurrentInstance();

//定义 $attrs
const $attrs = useAttrs();


//定义data
const $data = reactive({
  dlg_visible: false,
  version: 0,
  test: {},
  editorError: false,
  coderType: "Editor",
  info: {
    editors: [],
    templateContent: "",
    dockerfile: ""
  }, //子对象需要声明。
});
const info = $computed(() => $data.info);

const $props = defineProps({
  action: {type: String},
  id: {type: String}
});


const list = ref();

//定义事件
const $emit = defineEmits(["update:modelValue", "loaded"]);


const action_name = $computed(function () {
  return {add: "添加", edit: "修改"}[$props.action]
});

const versions = $computed(function () {
  return jv.ops.versions[$data.info.language] || [];
});

const loadData = async function () {
  if (!$props.id) return;
  var res = await jv.$http.post("/dev/user-dockerfile/detail/" + $props.id)
  $data.info = res.data.data;
};
// const computeEditorResult = function() {
//     if (!$data.info.editors.length) {
//         $data.info.dockerfile = "请添加编辑项！";
//         $data.editorError = true;
//         return;
//     }
//     var lines = $data.info.templateContent.split("\n");
//     var msg = "";
//     $data.info.editors.forEachIndexed((editor, index) => {
//         editor.matchLines = jv.dev.getEditorIndexes(lines, editor);
//         if (editor.matchLines.length !== 1) {
//             msg = "第 " + (index + 1) + " 项中匹配了 [" + editor.matchLines.map(it => it + 1).join(",") + "]行的数据，不能提交！";
//             return false;
//         }
//         var lineResult = jv.dev.getEditorResult(lines[editor.matchLines[0]], editor);
//         if (lineResult.msg) {
//             msg = lineResult.msg;
//             return false;
//         }
//         lines[editor.matchLines[0]] = lineResult;
//     });
//     if (!msg) {
//         $data.info.dockerfile = lines.join("\n");
//         $data.editorError = false;
//         return;
//     } else {
//         $data.info.dockerfile = msg;
//         $data.editorError = true;
//         return;
//     }
// };
const save_click = async function () {
//校验
  if (jv.chk_main($data) == false) {
    return;
  }
  // computeEditorResult();
  // if ($data.editorError) {
  //     return;
  // }
  var res = await jv.$http.post("/dev/user-dockerfile/save", $data.info)
//[axios拦截器中已处理了异常]。
  jv.info(action_name + " 成功");
  if ($props.action == "add") {
    var id = res.data.data
    jv.$router.push("/dev/user-dockerfile/edit/" + id)
  } else if ($props.action == "edit") {
    jv.$router.push("/dev/user-dockerfile/list")
  }
};
const version_change = function () {

  jv.$http.post("/ops/jenkins/default-dockerfile",
      {
        language: $data.info.language,
        languageVersion: $data.info.languageVersion
      }
  ).then(res => {
    $data.version++;
    $data.info.templateContent = res.data.data;
  })
};
// const tab_clik = function(tab) {
//     if (tab.paneName == "Text") {
//         computeEditorResult();
//     }
// };

onMounted(function () {
  loadData();
})


</script>
